package com.cyw.common.utils;


import com.cyw.common.domain.common.ListWrapper;
import com.cyw.common.domain.common.PageWrapper;
import com.cyw.common.exceptions.BaseException;

import java.util.*;

import static java.util.Optional.empty;
import static java.util.Optional.of;

public class BaseUtils {


    public static void thrBaseException(String mes) {
        throw new BaseException(mes);
    }

    public static void isThr(Boolean b, String errMsg) {
        if (b) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotThr(Boolean b, String errMsg) {
        if (!b) {
            thrBaseException(errMsg);
        }
    }

    public static Boolean isSame(Object obj, Object obj1) {
        return Objects.equals(obj, obj1);
    }

    public static void isSameThr(Object obj, Object obj1, String errMsg) {
        if (isSame(obj, obj1)) {
            thrBaseException(errMsg);
        }
    }

    public static Boolean isNotSame(Object obj, Object obj1) {
        return !isSame(obj, obj1);
    }

    public static void isNotSameThr(Object obj, Object obj1, String errMsg) {
        if (isNotSame(obj, obj1)) {
            thrBaseException(errMsg);
        }
    }

    public static Boolean isNull(Object object) {
        return Objects.isNull(object);
    }

    public static Boolean isNotNull(Object object) {
        return Objects.nonNull(object);
    }

    public static void isNotNullThr(Object object, String errMsg) {
        if (isNotNull(object)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNullThr(Object object, String errMsg) {
        if (isNull(object)) {
            thrBaseException(errMsg);
        }
    }

    /**
     * 判断其中某个元素是否为空
     * <p>
     * isAnyNull(1,null)    --> true
     * isAnyNull(1,1)       --> false
     * isAnyNull(1,"1")     --> false
     * isAnyNull(1,"null")  --> false
     *
     * @param objects
     * @return
     */
    public static Boolean isAnyNull(Object... objects) {
        if (isNull(objects) || objects.length == 0) {
            return true;
        }

        for (Object o : objects) {
            if (isNull(o)) {
                return true;
            }
        }
        return false;
    }

    public static void isAnyNullThr(String errMsg, Object... objects) {
        Boolean tag = isAnyNull(objects);
        if (tag) {
            thrBaseException(errMsg);
        }
    }

    /**
     * 判断可变参数中元素是否全为空
     *
     * @param objects
     * @return
     */
    public static Boolean isAllNull(Object... objects) {
        if (isNull(objects) || objects.length == 0) {
            return true;
        }

        for (Object o : objects) {
            if (isNotNull(o)) {
                return false;
            }
        }
        return true;
    }

    public static void isAllNullThr(String errMsg, Object... objects) {
        Boolean tag = isAllNull(objects);
        if (tag) {
            thrBaseException(errMsg);
        }
    }

    public static boolean isEmpty(ListWrapper<?> listWrapper) {
        return null == listWrapper || listWrapper.isEmpty();
    }

    public static boolean isEmpty(PageWrapper<?> pageWrapper) {
        return null == pageWrapper || pageWrapper.isEmpty();
    }

    public static boolean isEmpty(Iterable<?> iterable) {
        return null == iterable || isEmpty(iterable.iterator());
    }

    public static boolean isEmpty(Iterator<?> Iterator) {
        return null == Iterator || !Iterator.hasNext();
    }

    public static boolean isNotEmpty(PageWrapper<?> pageWrapper) {
        return null != pageWrapper && !pageWrapper.isEmpty();
    }

    public static boolean isNotEmpty(Iterable<?> iterable) {
        return null != iterable && isNotEmpty(iterable.iterator());
    }

    public static boolean isNotEmpty(Iterator<?> Iterator) {
        return null != Iterator && Iterator.hasNext();
    }

    public static void isEmptyThr(Iterator<?> collection, String errMsg) {
        if (isEmpty(collection)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotEmptyThr(Iterator<?> collection, String errMsg) {
        if (isNotEmpty(collection)) {
            thrBaseException(errMsg);
        }
    }

    public static void isEmptyThr(Iterable<?> collection, String errMsg) {
        if (isEmpty(collection)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotEmptyThr(Iterable<?> collection, String errMsg) {
        if (isNotEmpty(collection)) {
            thrBaseException(errMsg);
        }
    }

    public static Boolean isEmpty(Map<?, ?> map) {
        return isNull(map) || map.isEmpty();
    }

    public static Boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static void isEmptyThr(Map<?, ?> map, String errMsg) {
        if (isEmpty(map)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotEmptyThr(Map<?, ?> map, String errMsg) {
        if (isNotEmpty(map)) {
            thrBaseException(errMsg);
        }
    }

    public static Boolean isEmpty(Object[] objects) {
        return objects == null || objects.length == 0;
    }

    public static Boolean isNotEmpty(Object[] objects) {
        return !isEmpty(objects);
    }

    public static void isEmptyThr(Object[] objects, String errMsg) {
        if (isEmpty(objects)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotEmptyThr(Object[] objects, String errMsg) {
        if (isNotEmpty(objects)) {
            thrBaseException(errMsg);
        }
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }


    public static Boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static Boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static Boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static void isEmptyThr(String str, String errMsg) {
        if (isEmpty(str)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNotEmptyThr(String str, String errMsg) {
        if (isNotEmpty(str)) {
            thrBaseException(errMsg);
        }
    }


    /**
     * 有空字符串
     * <p>
     * isAnyEmpty(null)             = true
     * isAnyEmpty(null, "foo")      = true
     * isAnyEmpty("", "bar")        = true
     * isAnyEmpty("bob", "")        = true
     * isAnyEmpty("  bob  ", null)  = true
     * isAnyEmpty(" ", "bar")       = false
     * isAnyEmpty("foo", "bar")     = false
     *
     * @param css
     * @return
     */
    public static Boolean isAnyEmpty(CharSequence... css) {
        for (CharSequence s : css) {
            if (isEmpty(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 全不为空
     * <p>
     * isNoneEmpty(null)             = false
     * isNoneEmpty(null, "foo")      = false
     * isNoneEmpty("", "bar")        = false
     * isNoneEmpty("bob", "")        = false
     * isNoneEmpty("  bob  ", null)  = false
     * isNoneEmpty(" ", "bar")       = true
     * isNoneEmpty("foo", "bar")     = true
     *
     * @param css
     * @return
     */
    public static Boolean isNoneEmpty(CharSequence... css) {
        return !isAnyEmpty(css);
    }

    public static void isAnyEmptyThr(String errMsg, CharSequence... css) {
        if (isAnyEmpty(css)) {
            thrBaseException(errMsg);
        }
    }

    public static void isNoneEmptyThr(String errMsg, CharSequence... css) {
        if (isNoneEmpty(css)) {
            thrBaseException(errMsg);
        }
    }

    public static <T> T selectOne(Collection<T> list) {
        return isEmpty(list) ? null : list.iterator().next();
    }

    public static <T extends Collection<?>> Optional<T> ofNullable(T value) {
        return value == null
                || isEmpty(value) ? empty() : of(value);
    }

    public static <T extends String> Optional<T> ofNullable(T value) {
        return value == null
                || isEmpty(value) ? empty() : of(value);
    }

    public static <T extends Map<?, ?>> Optional<T> ofNullable(T value) {
        return value == null
                || isEmpty(value) ? empty() : of(value);
    }

    /**
     * 是否“正”id
     * 仅支持Intege和Long
     *
     * @param number
     * @return
     */
    public static boolean positiveId(Number number) {
        if (isNull(number)) {
            return false;
        }
        if (number instanceof Integer) {
            return number.intValue() > 0;
        }
        if (number instanceof Long) {
            return number.longValue() > 0;
        }
        throw new BaseException("不支持的类型");
    }

}